/**HEADER********************************************************************
* 
* Copyright (c) 2010 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2010 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: mpc83xx_etsec_send.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*            This file contains the mpc8349 Ethernet send
*            support function.
*
*END************************************************************************/

#include <mqx.h>
#include <bsp.h>
#include <enet.h>
#include <enetprv.h>
#include <mpc83xx_etsec_prv.h>

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_tx_record
*  Returned Value : void
*  Comments       :
*        Records statistics following frame transmission.
*
*END*-----------------------------------------------------------------*/

void MPC83xx_etsec_tx_record
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR  enet_ptr,

      /* [IN] the descriptor status word (all buffers) */
      uint_32              status_all,

      /* [IN] the descriptor status word (last buffer) */
      uint_32              status_last
   )
{ 
   uint_32  coll;

#if BSPCFG_ENABLE_ENET_STATS
   enet_ptr->STATS.COMMON.ST_TX_TOTAL++;

    if (status_all & MPC83xx_TSEC_BD_TX_ERROR)
   {

      /* If an error occurred, record it */
      enet_ptr->STATS.COMMON.ST_TX_ERRORS++;
      if (status_all & MPC83xx_TSEC_BD_TX_UNDERRUN) {
         enet_ptr->STATS.ST_TX_UNDERRUN++;
      } 
      if (status_all & MPC83xx_TSEC_BD_TX_FAILED) {
         enet_ptr->STATS.ST_TX_EXCESSCOLL++;
      } 
      if (status_all & MPC83xx_TSEC_BD_TX_LATE_COLLISION) {
         enet_ptr->STATS.ST_TX_LATECOLL++;
      } 

   } else {
      #ifndef BSPCFG_ENABLE_ENET_HISTOGRAM

      coll = (status_last & MPC83xx_TSEC_BD_TX_RETRY_MASK) >> 2;
      enet_ptr->STATS.COMMON.TX_HISTORY[coll]++;
      #endif
   } 

   if (status_all & MPC83xx_TSEC_BD_TX_DEFERRED) {
      enet_ptr->STATS.ST_TX_DEFERRED++;
   } 

#endif
} 


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_tx_catchup
*  Returned Value : void
*  Comments       :
*        Processes transmitted packets.
*
*END*-----------------------------------------------------------------*/

void MPC83xx_etsec_tx_catchup
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR  enet_ptr
   )
{ 
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR    tsec_context_ptr;
   VMPC83xx_TSEC_BD_STRUCT_PTR        bd_ptr;
   PCB_FRAGMENT_PTR                   frag_ptr;
   PCB_PTR                            pcb_ptr;
   uint_32                            status = 0;
   boolean                            restart = FALSE;
   volatile uint_32                   i;

   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;

   /* Dequeue all fully transmitted frames */
   bd_ptr = &tsec_context_ptr->TSEC_TX_RING_PTR[tsec_context_ptr->LastTxBD];
   _DCACHE_INVALIDATE_MLINES((void *)bd_ptr, sizeof(VMPC83xx_TSEC_BD_STRUCT));
   
   while (tsec_context_ptr->AvailableTxBDs < BSPCFG_TX_RING_LEN) {
      if (bd_ptr->CONTROL & MPC83xx_TSEC_BD_TX_READY) {
         break;
      } 
      tsec_context_ptr->TxErrors |= bd_ptr->CONTROL;
      status = tsec_context_ptr->TxErrors;

      /* Record statistics for each frame (not each buffer) */
      if (status & MPC83xx_TSEC_BD_TX_LAST_IN_FRAME) {
         pcb_ptr = tsec_context_ptr->TxPCBS_PTR[tsec_context_ptr->LastTxBD];
         tsec_context_ptr->TxErrors = 0;

         MPC83xx_etsec_tx_record(enet_ptr, status, bd_ptr->CONTROL);

         PCB_free(pcb_ptr);
      } 

      bd_ptr->CONTROL &= (MPC83xx_TSEC_BD_TX_INTERRUPT |
                          MPC83xx_TSEC_BD_TX_PAD       | 
                          MPC83xx_TSEC_BD_TX_CRC       |
                          MPC83xx_TSEC_BD_TX_WRAP);

      _DCACHE_FLUSH_MLINES((void *)bd_ptr, sizeof(VMPC83xx_TSEC_BD_STRUCT));                          

      TX_INC(tsec_context_ptr->LastTxBD);
      tsec_context_ptr->AvailableTxBDs++;

      bd_ptr = &tsec_context_ptr->TSEC_TX_RING_PTR[tsec_context_ptr->LastTxBD];
      _DCACHE_INVALIDATE_MLINES((void *)bd_ptr, sizeof(VMPC83xx_TSEC_BD_STRUCT));
      if (status & MPC83xx_TSEC_BD_TX_LAST_IN_FRAME) {
         if (bd_ptr->CONTROL & MPC83xx_TSEC_BD_TX_READY) {
            break;
         } 
      }
   }
} 



/*ISR*------------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_TX_ISR
*  Returned Value : void
*  Comments       :
*        Transmit Unit ISR
*
*END*-----------------------------------------------------------------*/

void MPC83xx_etsec_TX_ISR
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR  enet_ptr
   )
{ 
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR          tsec_ptr;
   uint_32                          events;

   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;
   
   while (TRUE) {
      events = tsec_ptr->IEVENT;
      if (events & (MPC83xx_TSEC_IEVENT_TXB | MPC83xx_TSEC_IEVENT_TXF)) {
         MPC83xx_etsec_tx_catchup(enet_ptr);
         tsec_ptr->IEVENT =(MPC83xx_TSEC_IEVENT_TXB | MPC83xx_TSEC_IEVENT_TXF);
         _PSP_SYNC();
      } else {
      	break;
      }
   } /* Endwhile */
   
} 


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_send_MAC
*  Returned Value : ENET_OK or error code
*  Comments       :
*        Sends a packet.
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_send_MAC
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR  enet_ptr,

      /* [IN] the packet to send */
      PCB_PTR              packet,

      /* [IN] total size of the packet */
      uint_32              size,

      /* [IN] total fragments in the packet */
      uint_32              frags,

      /* [IN] optional flags, zero = default */
      uint_32              flags
   )
{ /* Body */
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR          tsec_ptr;
   PCB_FRAGMENT_PTR                 frag_ptr;
   VMPC83xx_TSEC_BD_STRUCT_PTR      txring;
   uint_32                          txfirst;
   
   etsec_lock();

   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;
   
   /*
   ** Make sure there aren't too many fragments.  (We really should check
   ** this every time through the previous loop, but it is extremely
   ** unlikely that the fragment counter overflowed -- there would have
   ** to be over 2^32 fragments.)
   */
   if (frags > tsec_context_ptr->AvailableTxBDs) {
      ENET_INC_STATS(COMMON.ST_TX_MISSED);
      return ENETERR_SEND_LONG;
   } /* Endif */

   /*
   ** Enqueue the packet on the transmit ring.  Don't set the ready
   ** bit in the first descriptor until all descriptors are enqueued.
   */
   txfirst = tsec_context_ptr->NextTxBD;
   txring  = tsec_context_ptr->TSEC_TX_RING_PTR;
   
   _DCACHE_INVALIDATE_MLINES((void *)txring, sizeof(VMPC83xx_TSEC_BD_STRUCT) * BSPCFG_TX_RING_LEN);
   for (frag_ptr = packet->FRAG;;) {
      /*
      ** The QUICC doesn't clear control bits when errors don't occur;
      ** it just sets bits when errors do occur.  So we clear all the
      ** control bits before enqueueing each descriptor.
      */
      txring[tsec_context_ptr->NextTxBD].CONTROL &= 
          (MPC83xx_TSEC_BD_TX_INTERRUPT |
           MPC83xx_TSEC_BD_TX_PAD       | 
           MPC83xx_TSEC_BD_TX_CRC       |
           MPC83xx_TSEC_BD_TX_WRAP      | 
           MPC83xx_TSEC_BD_TX_READY);
      txring[tsec_context_ptr->NextTxBD].LENGTH = frag_ptr->LENGTH;
      txring[tsec_context_ptr->NextTxBD].BUFFER = frag_ptr->FRAGMENT;
      _dcache_flush_mlines(frag_ptr->FRAGMENT, frag_ptr->LENGTH);
      tsec_context_ptr->AvailableTxBDs--;

      frag_ptr++;
      if (frag_ptr->LENGTH == 0)  {
         break;
      } /* Endif */

      TX_INC(tsec_context_ptr->NextTxBD);
      txring[tsec_context_ptr->NextTxBD].CONTROL |= MPC83xx_TSEC_BD_TX_READY;
   } /* Endfor */

   txring[tsec_context_ptr->NextTxBD].CONTROL |= MPC83xx_TSEC_BD_TX_LAST_IN_FRAME;
   _DCACHE_FLUSH_MLINES((void *)txring, sizeof(VMPC83xx_TSEC_BD_STRUCT) * BSPCFG_TX_RING_LEN);   
   if(tsec_ptr->TSTAT & MPC83xx_TSEC_TSTAT_THLT)
      tsec_ptr->TSTAT = MPC83xx_TSEC_TSTAT_THLT;
   
   txring[txfirst].CONTROL |= MPC83xx_TSEC_BD_TX_READY;
   _DCACHE_FLUSH_MLINES((void *)&txring[txfirst], sizeof(VMPC83xx_TSEC_BD_STRUCT));   
   tsec_context_ptr->TxPCBS_PTR[tsec_context_ptr->NextTxBD] = packet;
   TX_INC(tsec_context_ptr->NextTxBD);

   etsec_unlock();
   return ENET_OK;

} /* Endif */

/* EOF */
